package yxy.game.pm2.command.pickedtroops;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

/****************************************
 * @name CMD_PickedTroopsSlotChange
 * @description 兵符替换
 * @author 梁健彬
 * @since 2022-05-20
 ***************************************/
@MessageType(proto = Command.PickedTroopsSlotChange, classtype = PickedTroopsMessage.PickedTroopsSlotChangeRequest.class)
public class CMD_PickedTroopsSlotChange extends Command<PickedTroopsMessage.PickedTroopsSlotChangeRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotChangeRequest request) {
        String playerCode = messageSource.getTag();
        int camp = request.getCamp();
        String slotCode = request.getSlotCode();

        PickedTroopsMessage.PickedTroopsSlotChangeResponse.Builder response = PickedTroopsMessage.PickedTroopsSlotChangeResponse.newBuilder();

        try {
            //锁兵符和兵符槽缓存
            if (PickedTroopsModule.lockPickedTroops(playerCode) && PickedTroopsSlot.lockPickedTroopsSlot(playerCode)) {

                PickedTroopsModule module = PickedTroopsModule.get(playerCode);

                //判断兵符是否存在
                PickedTroopsSlot slot = PickedTroopsSlot.get(playerCode, slotCode);
                if (slot == null) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                //兵符是否属于该阵营
                if (slot.getType() != camp) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                //替换兵符,装备上的兵符卸下
                PickedTroops pickedTroops = module.getPts().get(camp);
                String oldSlotCode = pickedTroops.getPickedTroopsSolt();
                pickedTroops.setPickedTroopsSolt(slotCode);
                PickedTroopsSlot oldSlot = PickedTroopsSlot.get(playerCode, oldSlotCode);
                oldSlot.setUse(false);
                slot.setUse(true);

                PickedTroopsSlot.set(playerCode, slot);
                PickedTroopsSlot.set(playerCode, oldSlot);
                module.set();

                //返回兵符的全部信息
                for (PickedTroops pts : module.getPts().values()) {
                    response.addPts(PickedTroopsHandle.getPickedTroops(playerCode, pts));//返回全部兵符
                }

                //更新战力
                PlayTypeHandle.savePickedTroop(playerCode, PlayTypeModule.get(playerCode));
                new BattleHandle().powerUpdate(playerCode);// 战力更新

                send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            //释放锁
            PickedTroopsModule.unlockPickedTroops(playerCode);
            PickedTroopsSlot.unlockPickedTroopsSlot(playerCode);
        }
    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotChangeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}